package cn.mickey.exchange.api;


import cn.hutool.core.lang.UUID;
import cn.mickey.exchange.domain.JsonResponse;
import cn.mickey.exchange.properties.LocalUploadProperties;
import cn.mickey.exchange.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@Slf4j
@RestController
public class FileApi {

    @Resource
    private LocalUploadProperties localUploadProperties;

    @Resource
    private FileService fileService;

    /**
     * 生成文件名
     *
     * @param multipartFile
     * @return
     */
    public String getFileName(MultipartFile multipartFile) {
        //获取上传文件的文件名
        String multipartFileName = multipartFile.getOriginalFilename();
        //获取文件后缀名
        String suffixName = multipartFileName.substring(multipartFileName.lastIndexOf("."));
        //通过uuid生成不重复的文件名
        return UUID.fastUUID() + suffixName;
    }

    /**
     * 文件上传
     * @param multipartFile
     * @return
     */
    @PostMapping("/file")
    public JsonResponse uploadFile(@RequestParam("file") MultipartFile multipartFile){
        //选择本地上传还是阿里oss云存储
        if (localUploadProperties.isEnabledLocal()){
            return localUploadFile(multipartFile);
        }
        return ossUploadFile(multipartFile);
    }

    /**
     * 上传到阿里oss云存储
     *
     * @param multipartFile
     * @return
     */
    public JsonResponse ossUploadFile(@RequestParam("file") MultipartFile multipartFile) {
        if (multipartFile.isEmpty()) {
            String url = fileService.uploadImage(getFileName(multipartFile), multipartFile);
            if (url != null) {
                return new JsonResponse(url, 1, "上传成功!");
            }
        }
        return JsonResponse.fail("文件上传失败!");
    }

    /**
     * 上传到本地
     *
     * @param multipartFile
     * @return
     */
    public JsonResponse localUploadFile(@RequestParam("file") MultipartFile multipartFile) {
        String fileName = getFileName(multipartFile);
        try {
            if (fileService.uploadFile(multipartFile, fileName)) {
                return new JsonResponse(localUploadProperties.getBaseUrl() + "/image?imageName=" + fileName, 1, "上传成功!");
            }
        } catch (IOException e) {
            log.error(e.getMessage());
            return JsonResponse.fail("系统错误!");
        }
        return JsonResponse.fail("文件上传失败!");
    }

    /**
     * 从本地获取图片
     * @param imageName
     * @param response
     * @throws IOException
     */
    @GetMapping("/image")
    public void getLocalImage(@RequestParam("imageName") String imageName,
                         HttpServletResponse response) throws IOException {
        File image = new File(localUploadProperties.getUserFilePath() + "/" + imageName);
        if (image.exists()) {
            FileInputStream fileInputStream = new FileInputStream(image);
            byte[] bytes = new byte[fileInputStream.available()];
            if (fileInputStream.read(bytes) > 0) {
                OutputStream outputStream = response.getOutputStream();
                outputStream.write(bytes);
                outputStream.flush();
                outputStream.close();
            }
            fileInputStream.close();
        }
    }

}
